package com.cqust.icc.demo7;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/4/1 16:55
 */

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "HandleData", value = "/handleData")
public class HandleData extends HttpServlet {
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Equation equ = new Equation();  //创建Javabean对象
        request.setAttribute("equation", equ);// 将equ存储到HttpServletRequest对象中
        double a = Double.parseDouble(request.getParameter("a"));
        double b = Double.parseDouble(request.getParameter("b"));
        double c = Double.parseDouble(request.getParameter("c"));
        equ.setA(a);            //将数据存储在equ中
        equ.setB(b);
        equ.setC(c);
        if (a != 0)
            equ.setIsSquareEquation(true);
        else
            equ.setIsSquareEquation(true);
        double disk = b * b - 4 * a * c;
        if (disk < 0) {
            equ.setRootOne("无实根");
            equ.setRootTwo("无实根");
        } else {
            double root1 = (-b + Math.sqrt(disk)) / (2 * a),
                    root2 = (-b - Math.sqrt(disk)) / (2 * a);
            equ.setRootOne("" + root1);
            equ.setRootTwo("" + root2);
        }
        RequestDispatcher dispatcher = request.getRequestDispatcher("show.jsp");
        dispatcher.forward(request, response);    //请求show.jsp显示equ中的数据
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}
